//
//  OrderDetailView.m
//  HAODE
//
//  Created by panquanReabam.com on 16/5/27.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "OrderDetailView.h"
#import "payView.h"
@interface OrderDetailView()
@property(nonatomic,strong)UIView*amountView;
@property(nonatomic,strong)UILabel*amount;
@property(nonatomic,strong)UILabel*amountLabel;
@property(nonatomic,strong)UILabel*preferential;
@property(nonatomic,strong)UILabel*preferentialLabel;
@property(nonatomic,strong)UILabel*realPaymentLabel;
@property(nonatomic,strong)UIView*payView;
@property(nonatomic,strong)UILabel*pay;
@property(nonatomic,strong)UILabel*payLabel;
@property(nonatomic,strong)UILabel*informationLabel;
@property(nonatomic,strong)UIView*instructionsView;
@property(nonatomic,strong)UILabel*instructions;
@property(nonatomic,strong)UILabel*instructionsLabel;

@end
@implementation OrderDetailView
{
    NSMutableArray*payment;
    payView*zhifuviews;
    NSString*payType;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
    }
    return self;
}
#pragma mark InitUI
- (void)initUI
{
    self .backgroundColor = [UIColor whiteColor];
    _amountView=[[UIView alloc]init];
    _amountView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_amountView];
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 100, ScreenWidth, 10)];
    view.backgroundColor=zuiqianhuiColor;
    view.layer.borderColor = qianhuiColor.CGColor;
    [view.layer setBorderWidth:0.35];
    [self addSubview:view];
    _amount=[[UILabel alloc]init];
    _amount.textColor=shenhuiColor;
    _amount.text=@"商品总额";
    _amount.font=[UIFont systemFontOfSize:15];
    [self addSubview:_amount];
    _amountLabel=[[UILabel alloc]init];
    _amountLabel.textColor=shenhuiColor;
    _amountLabel.font=[UIFont systemFontOfSize:15];
    [self addSubview:_amountLabel];
    _preferential=[[UILabel alloc]init];
    _preferential.textColor=shenhuiColor;
     _preferential.text=@"优惠";
    _preferential.font=[UIFont systemFontOfSize:15];
    [self addSubview:_preferential];
    _preferentialLabel=[[UILabel alloc]init];
    _preferentialLabel.textColor=shenhuiColor;
    _preferentialLabel.font=[UIFont systemFontOfSize:15];
    [self addSubview:_preferentialLabel];
    _realPaymentLabel=[[UILabel alloc]init];
    _realPaymentLabel.textColor=hongseColor;
    _realPaymentLabel.font=[UIFont systemFontOfSize:18];
    [self addSubview:_realPaymentLabel];
    
    _payView=[[UIView alloc]init];
    _payView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_payView];
    _pay=[[UILabel alloc]init];
    _pay.text=@"支付信息";
    _pay.textColor=shenhuiColor;
    _pay.font=[UIFont systemFontOfSize:15];
    [self addSubview:_pay];
    _payLabel=[[UILabel alloc]init];
    _payLabel.textColor=shenhuiColor;
    _payLabel.font=[UIFont systemFontOfSize:15];
    [self addSubview:_payLabel];

    _heightView=[[UIView alloc]init];
    _heightView.backgroundColor=qianhuiColor;
    [self addSubview:_heightView];
    
}

-(void)setModel:(OrderDetailModel *)model{
    [self initUI];
    [self setViewAutoLayout];
   
    NSNumber*Total = [[model.dict objectForKey:@"order"] objectForKey:@"totalMoney"];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:Total.doubleValue]];
    self.amountLabel.text=[NSString  stringWithFormat:@"¥ %@", formattedNumberString];
    self.amountLabel.textAlignment = NSTextAlignmentRight;
    
    NSNumber*couponMoney = [[model.dict objectForKey:@"order"] objectForKey:@"couponMoney"];
    NSNumberFormatter *numberFormatter2 = [[NSNumberFormatter alloc] init];
    [numberFormatter2 setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString2 = [numberFormatter2 stringFromNumber:[NSNumber numberWithDouble:couponMoney.doubleValue]];
    self.preferentialLabel.text=[NSString stringWithFormat:@"－ ¥ %@",   formattedNumberString2];
    self.preferentialLabel.textAlignment = NSTextAlignmentRight;
   
    
    NSNumber*realMoney = [[model.dict objectForKey:@"order"] objectForKey:@"realMoney"];
    NSNumberFormatter *numberFormatter1 = [[NSNumberFormatter alloc] init];
    [numberFormatter1 setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString1 = [numberFormatter1 stringFromNumber:[NSNumber numberWithDouble:realMoney.doubleValue]];

    self.realPaymentLabel.text=[NSString stringWithFormat:@"实付款  ¥ %@",   formattedNumberString1];
    self.realPaymentLabel.textColor=hongseColor;
    self.realPaymentLabel.font=[UIFont systemFontOfSize:18];
    self.realPaymentLabel.textAlignment = NSTextAlignmentRight;
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"实付款  ¥ %@" ,formattedNumberString1]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:13.0]
     
                          range:NSMakeRange(0, 4)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor blackColor]
     
                          range:NSMakeRange(0, 4)];
    self.realPaymentLabel.attributedText = AttributedStr;


   
    payment=[model.dict objectForKey:@"payment"];
    if (payment.count>0) {
        for (int i = 0; i<payment.count; i++) {
            if ([[[payment objectAtIndex:i] objectForKey:@"payType"]isEqualToString:@"CashPay"]) {
                payType=@"现金";
            }
            if ([[[payment objectAtIndex:i] objectForKey:@"payType"]isEqualToString:@"IboxPay"]) {
                payType=@"银联";
            }
            if ([[[payment objectAtIndex:i] objectForKey:@"payType"]isEqualToString:@"AliPay"]) {
                payType=@"支付宝";
            }
            if ([[[payment objectAtIndex:i] objectForKey:@"payType"]isEqualToString:@"wx_pay"]) {
                payType=@"微信";
            }
            zhifuviews = [[payView alloc]initWithFrame:CGRectMake(0, 0+(15*i), ScreenWidth, contenfot)];
            if ([[[payment objectAtIndex:i] objectForKey:@"gatheringType"]isEqualToString:@"F"]) {
                
                zhifuviews.gatheringType.text=[NSString stringWithFormat:@"%@ ¥ %@  %@",  @"全款",[[payment objectAtIndex:i] objectForKey:@"payAmount"],payType];
            }
            if ([[[payment objectAtIndex:i] objectForKey:@"gatheringType"]isEqualToString:@"E"]) {
                
                zhifuviews.gatheringType.text=[NSString stringWithFormat:@"%@ ¥ %@  %@",  @"定金",[[payment objectAtIndex:i] objectForKey:@"payAmount"],payType];
            }
            if ([[[payment objectAtIndex:i] objectForKey:@"gatheringType"]isEqualToString:@"R"]) {
                zhifuviews.gatheringType.text=[NSString stringWithFormat:@"%@ ¥ %@  %@",  @"尾款",[[payment objectAtIndex:i] objectForKey:@"payAmount"],payType];
            }
            if ([[[payment objectAtIndex:i] objectForKey:@"gatheringType"]isEqualToString:@"O"]) {
                zhifuviews.gatheringType.text=[NSString stringWithFormat:@"%@ ¥ %@  %@",  @"优惠",[[payment objectAtIndex:i] objectForKey:@"payAmount"],payType];
            }
            NSRange range={0,16};
            NSString* string =[[NSString stringWithFormat:@"%@",[[payment objectAtIndex:i] objectForKey:@"createDate"]] substringWithRange:range];
            zhifuviews.createDate.text=[NSString stringWithFormat:@"%@",string];
            [_payView addSubview:zhifuviews];
            
        }
          _payView.frame=CGRectMake(0, 110+50, ScreenWidth, zhifuviews.bottom+20);
    }else{
        
        UILabel*zhifuxinxi1=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-150, 0, 130, 45)];
        zhifuxinxi1.text=@"暂无支付";//
        zhifuxinxi1.textColor=shenhongseColor;
        zhifuxinxi1.textAlignment = NSTextAlignmentRight;
        zhifuxinxi1.font=[UIFont systemFontOfSize:contenfot];
        [_payView addSubview:zhifuxinxi1];
        UILabel*zhifuxinxi2=[[UILabel alloc]initWithFrame:CGRectMake(15, 45, 130, contenfot)];
        zhifuxinxi2.text=@"定金支付日期";
        zhifuxinxi2.textColor=shenhuiColor;
        zhifuxinxi2.font=[UIFont systemFontOfSize:contenfot];
        [_payView addSubview:zhifuxinxi2];
        UILabel*zhifuxinxi3=[[UILabel alloc]initWithFrame:CGRectMake(15, 70, 130, contenfot)];
        zhifuxinxi3.text=@"尾款支付日期";
        zhifuxinxi3.textColor=shenhuiColor;
        zhifuxinxi3.font=[UIFont systemFontOfSize:contenfot];
        [_payView addSubview:zhifuxinxi3];
         _payView.frame=CGRectMake(0, 110+50, ScreenWidth, zhifuxinxi3.bottom+20);
        
    }
  
    UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(0, _payView.bottom, ScreenWidth, 10)];
    view1.backgroundColor=zuiqianhuiColor;
    view1.layer.borderColor = qianhuiColor.CGColor;
    [view1.layer setBorderWidth:0.35];   //边
    [self addSubview:view1];
    _instructionsView=[[UIView alloc]initWithFrame:CGRectMake(0, view1.bottom, ScreenWidth, 200)];
    _instructionsView.backgroundColor=[UIColor whiteColor];
    _instructionsView.layer.borderWidth = 0.35;
    _instructionsView.layer.borderColor = qianhuiColor.CGColor;
    [self addSubview:_instructionsView];
    _instructions=[[UILabel alloc]initWithFrame:CGRectMake(15, 15, ScreenWidth*70/750, 15)];
    _instructions.text=@"说明";
    _instructions.font= [UIFont systemFontOfSize:15];
    [_instructions setTextColor:shenhuiColor];
    [_instructionsView addSubview:_instructions];
    
    if ([[NSString stringWithFormat:@"%@",[[model.dict objectForKey:@"order"] objectForKey:@"remark"]] length]>1) {
        NSString *s1 =[NSString stringWithFormat:@"%@", [NSString stringWithFormat:@"%@",[[model.dict objectForKey:@"order"] objectForKey:@"remark"]]];
        UIFont *font1 = [UIFont systemFontOfSize:contenfot];
        CGSize size1 = CGSizeMake(ScreenWidth-60, 2000);
        CGSize labelsize1 = [s1 sizeWithFont:font1 constrainedToSize:size1 lineBreakMode:NSLineBreakByCharWrapping];
        UILabel*instructionsLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, _instructionsView.bottom+12, ScreenWidth-40, labelsize1.height+30)];
        instructionsLabel.text=s1;
        instructionsLabel.numberOfLines=0;
        instructionsLabel.font= [UIFont systemFontOfSize:contenfot];
        [instructionsLabel setTextColor:shenhuiColor];
        [_instructionsView addSubview:instructionsLabel];
        _instructionsView.frame=CGRectMake(0, _payView.bottom+10, ScreenWidth, instructionsLabel.bottom+120);
    }else{
        _instructionsView.frame=CGRectMake(0, _payView.bottom+10, ScreenWidth, _instructionsView.bottom+120);
        [self addSubview:_instructionsView];
        
    }
    
    _heightView.frame=CGRectMake(0, _instructionsView.bottom, ScreenWidth, 0.15);


}
-(void)setViewAutoLayout
{
    [self.amountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(0) ;
        make.height.equalTo(@100);
        make.top.equalTo(self.mas_top).with.offset(0);
    }];
    [self.amount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(15) ;
        make.height.equalTo(@15);
        make.top.equalTo(self.mas_top).with.offset(15);
    }];
    [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(-15);
        make.height.equalTo(@15);
        make.centerY.equalTo(self.amount.mas_centerY) ;
    }];
   
    [self.preferential mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(15);
        make.height.equalTo(@15);
        
        make.top.equalTo(self.amount.mas_bottom).with.offset(7.5);
    }];
    [self.preferentialLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(-15);
        make.height.equalTo(@15);
        
        make.centerY.equalTo(self.preferential.mas_centerY) ;
    }];

    [self.realPaymentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(-15);
        make.height.equalTo(@18);
        make.top.equalTo(self.preferentialLabel.mas_bottom).with.offset(15);
    }];
    
    
       [self.pay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(15);
        make.height.equalTo(@50);
        
        make.top.equalTo(self.amountView.mas_bottom).with.offset(10);
    }];

    
    
}


@end
